package com.pilipili.config;

import com.pilipili.pojo.User;
import com.pilipili.service.userService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    userService userService;

    @Override  //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override  //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken)token;
        User user = new User();
        user.setUser_id(usernamePasswordToken.getUsername());
        List<User> users = userService.selectUser(user);
        if (users.size()==0){
            return null;
        }
        return new SimpleAuthenticationInfo("",users.get(0).getUser_password(),"");
    }
}
